package menu;

import java.util.LinkedList;

import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import main.Application;
import main.UsersManager;
import menuaction.ConfigWindow;
import menuaction.FileLoader;
import menuaction.Kontakt;
import menuaction.OProgramie;
import menuaction.PasswordChanger;
import menuaction.RunBrowserMenuAction;
import menuaction.RunJasperRaportMenuAction;
import menuaction.ZboonSickExperimentsWithSwingMenuAction;
import menuaction.jasperTools.JasperParamsAdapter;
import menuaction.jasperTools.JasperRunnerParams;

import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class MenuAdapter {
	public final static String XML_NODE_NAME_MAINMENU = "mainMenu";
	public final static String XML_NODE_NAME_MENU = "menu";
	public final static String XML_NODE_NAME_MENUITEM = "menuItem";
	
	private JMenuBar menuBar;
	private JMenu menu;
	private JMenuItem menuItem;
	
	private LinkedList <MenuFieldAttributes> itemsToAdd;
	
	public MenuAdapter (String xmlFilePath) throws Exception 
	{
		menuBar = new JMenuBar();
		
		DOMParser xmlParser = new DOMParser();
		xmlParser.parse(xmlFilePath);
		Document doc = xmlParser.getDocument();
		createMenuBar(doc);
	}


	private void createMenuBar(Document doc) throws Exception
	{
		itemsToAdd = new LinkedList<MenuFieldAttributes>();		
		NodeList mainMenus = doc.getElementsByTagName(MenuAdapter.XML_NODE_NAME_MAINMENU);
		Element mainMenu = (Element) mainMenus.item(0);

		NodeList menus = mainMenu.getChildNodes();
		for (int i=0; i<menus.getLength(); ++i)
		{
			try
			{

				if(menus.item(i).getNodeName().equals("#text"))
					continue;
				Element menuElement = (Element) menus.item(i); 
				menu = createSubMenuFromMenuElement (menuElement);
				MenuFieldAttributes a = elementToMenuFieldAttributes (menuElement);
				if(!(a.isAdminOnly()&&!Application.getInstance().getLogin().equals(Application.getInstance().getAdminLogin())))
					menuBar.add(menu);
			}
			catch (Exception exception)
			{
				Application.getInstance().notifyAboutException(exception);
			}
		}

	}
	
	private JMenu createSubMenuFromMenuElement (Element e) throws Exception
	{		
		MenuFieldAttributes menAttrib = elementToMenuFieldAttributes (e);
		JMenu submenu = new JMenu(menAttrib.getLabel());

		ImageIcon icon = menAttrib.getPicture();
		if (icon != null)
			submenu.setIcon (icon);
		int keyMnemo = menAttrib.getMnemonic();
		if (keyMnemo != -1)
			submenu.setMnemonic(keyMnemo);

		NodeList subMenuItems = e.getChildNodes();
		for (int i = 0; i < subMenuItems.getLength(); ++i)
		{
			try
			{
				if(subMenuItems.item(i).getNodeName().equals("#text"))
					continue;
				Element item = (Element) subMenuItems.item(i);  
				if (item.getLocalName() == MenuAdapter.XML_NODE_NAME_MENUITEM)
				{
					MenuFieldAttributes mfa = elementToMenuFieldAttributes(item);
					menuItem = new JMenuItem (mfa.getLabel());
					if((mfa.isAdminOnly()&&!Application.getInstance().getLogin().equals(Application.getInstance().getAdminLogin())))
						continue;
					//jesli obecnie dodawany menuItem ma odpalac browsera i zdefiniowany tableName i wymiary
					//-------
					if (mfa.getActionName().equals(MenuFieldAttributes.ACTIONNAME_BROWSER) && mfa.getTableName() != "" 
						&& mfa.getTableWidth() != 0 && mfa.getTableHeight() != 0)
					{
						RunBrowserMenuAction brunner = new RunBrowserMenuAction (mfa.getTableName(), 
								mfa.getTableWidth(), mfa.getTableHeight());
						brunner.setTitle(mfa.getLabel());
						menuItem.addActionListener (brunner);
					}
					//-------
					else if (mfa.getActionName().equals(MenuFieldAttributes.ACTIONNAME_CHANGEPASSWORD))
													menuItem.addActionListener(new PasswordChanger());
					//-------					
					else if (mfa.getActionName().equals(MenuFieldAttributes.ACTIONNAME_MANAGEUSERS))
													menuItem.addActionListener(new UsersManager());
					//-------					
					else if (mfa.getActionName().equals(MenuFieldAttributes.ACTIONNAME_ZBOONEXPERIMENT1))
													menuItem.addActionListener(new ZboonSickExperimentsWithSwingMenuAction());
					//-------		
					else if (mfa.getActionName().equals(MenuFieldAttributes.ACTIONNAME_KONTAKT))
						menuItem.addActionListener(new Kontakt());
					//-------		
					else if (mfa.getActionName().equals(MenuFieldAttributes.ACTIONNAME_OPROGRAMIE))
						menuItem.addActionListener(new OProgramie());
					//	-------		
					
					else if (mfa.getActionName().equals(MenuFieldAttributes.ACTIONNAME_UPLOADFILE))
                         menuItem.addActionListener(new FileLoader());
					 
					else if (mfa.getActionName().equals(MenuFieldAttributes.ACTIONNAME_JASPER_RAPORT))
					{
						JasperParamsAdapter jasperReader = new JasperParamsAdapter(mfa.getRaportParamsSrc(),mfa.getRaportParamsName());
						JasperRunnerParams jasperParems = jasperReader.getParams();
						RunJasperRaportMenuAction jasperRunner = new RunJasperRaportMenuAction(jasperParems);
                        menuItem.addActionListener(jasperRunner);
					}
					else if (mfa.getActionName().equals(MenuFieldAttributes.ACTIONNAME_CONFIG))
					{
						menuItem.addActionListener(new ConfigWindow());
					}

					
					//*************
					ImageIcon itemIcon = mfa.getPicture();
					if (itemIcon != null)
						menuItem.setIcon (itemIcon);
					//*************
					int mnemo = mfa.getMnemonic();
					if (mnemo != -1)
						menuItem.setMnemonic(mnemo);
					//*************

					submenu.add(menuItem);
				}

				if (item.getLocalName() == MenuAdapter.XML_NODE_NAME_MENU)
				{
					JMenu newSubMenu = createSubMenuFromMenuElement (item);
					MenuFieldAttributes a = elementToMenuFieldAttributes (item);
					if(!(a.isAdminOnly()&&!Application.getInstance().getLogin().equals(Application.getInstance().getAdminLogin())))
						submenu.add(newSubMenu);					
				}					
			}
			catch (Exception exception)
			{
				//TODO won z ta dupa itd
			//	System.out.println("Dupa z konwersja2");
				Application.getInstance().getLogger().info("Dupa z konwersja2");
			}
		}
		return submenu;
	}
	
	private MenuFieldAttributes elementToMenuFieldAttributes (Element e) throws Exception
	{
		String label = e.getAttribute(MenuFieldAttributes.ATTRIBUTE_LABEL);
		String listenerName = e.getAttribute(MenuFieldAttributes.ATTRIBUTE_LISTENER_NAME);
		String actionName = e.getAttribute(MenuFieldAttributes.ATTRIBUTE_ACTIONNAME);
		String tableName = e.getAttribute(MenuFieldAttributes.ATTRIBUTE_TABLENAME);
		String tableHeight = e.getAttribute(MenuFieldAttributes.ATTRIBUTE_TABLEHEIGHT);
		String tableWidth = e.getAttribute(MenuFieldAttributes.ATTRIBUTE_TABLEWIDTH);
		String picture = e.getAttribute(MenuFieldAttributes.ATTRIBUTE_PICTURE);
		String mnemo = e.getAttribute(MenuFieldAttributes.ATTRIBUTE_MNEMONIC);
		String adminOnly = e.getAttribute(MenuFieldAttributes.ATTRIBUTE_ADMIN_ONLY);
		String raportParamsName = e.getAttribute(MenuFieldAttributes.ATTRIBUTE_JASPER_RAPORT_PARAMS_NAME);
		String raportParamsSrc = e.getAttribute(MenuFieldAttributes.ATTRIBUTE_JASPER_RAPORT_PARAMS_SRC);
		
		boolean boolAdminOnly = adminOnly.equals("true");
		
		MenuFieldAttributes mfa = new MenuFieldAttributes 
						(label, listenerName, actionName, tableName, tableHeight, tableWidth, picture, mnemo,raportParamsName,raportParamsSrc);
		mfa.setAdminOnly(boolAdminOnly);
		return mfa;
	}	
	public JMenuBar getMenuBar()
	{
		return menuBar;
	}
}
